package org.zoomdev.zoom.dao.datasource;

import org.zoomdev.zoom.dao.ConnectionProvider;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ThreadLocalRandom;

public final class MultiConnectionProvider implements ConnectionProvider {


    private final DataSource[] dataSources;

    public MultiConnectionProvider(DataSource... dataSources) {
        this.dataSources = dataSources;
    }

    @Override
    public Connection getConnection() throws SQLException {
        return getDataSource().getConnection();
    }

    @Override
    public DataSource getDataSource() {
        return dataSources[ThreadLocalRandom.current().nextInt(dataSources.length)];
    }
}
